//第一节练习
//1
let testStr="Hello World";
let testStrLow=testStr.toLowerCase();
console.log("问题",testStr)
console.log("答案",testStrLow)
console.log("----------------------------");
//2
let test2Str=require("./testData").content;
let test2StrArr=test2Str.match(/[A-Z]+/g);
console.log("大写字母数组",test2StrArr.length);
console.log("----------------------------");
//3
console.log("1+(2+3)*4/5-(6+7)");
let test3=1+(2+3)*4/5-(6+7);
console.log(test3)
console.log("----------------------------");
//4
function muti(arr){
  return  arr.reduce((a,b)=>a*b,1)

}
let testArr=[1,4,2,5,1,3,12,1]
console.log(testArr)
console.log(muti(testArr))
console.log("----------------------------");
/*---------------------------------------------------------------------*/
//第4节练习
//1
let test4arr=[1,2,3,4,5];
let test4zero=[0,0,0,0,0];
console.log(test4arr.map(item=>"a"+item));
// let str="";
console.log(test4arr.map((item,index)=>String.fromCharCode(97+index)+item));
console.log(test4arr.map(item=>Math.pow(item,2)))
console.log(test4zero.map((item,index)=>String.fromCharCode(65+index)));
console.log(test4arr.filter(item=>item>1&&item<5))
console.log("----------------------------");
/*---------------------------------------------------------------------*/
let _ = require('./tools/lodash.js');
let vote=require("./testData").vote;
function reduceByKey(tuples,reduceCallback) {
    let grouped=_.groupBy(tuples,tuple=>tuple[0]);
    return _.toPairs(_.mapValues(grouped,tuples=>_.chain(tuples).map(tuple=>tuple[1]).reduce(reduceCallback).value()))
}
let sortByDesc=function(arr,index){
    return arr.sort((b,a)=>a[index]-b[index]);
};

let count=vote.map(item=>[item,1]);

let countResult=reduceByKey(count,(left,right)=>left+right);
let sorted=sortByDesc(countResult,1)
let res=_.take(sorted,1).map(item=>{return {name:item[0],number:item[1]}})
console.log("投票结果",vote);
console.log("分析",sorted);
console.log("----------------------------")
/*---------------------------------------------------------------------*/

let work=require("./testData").work;
function translateWorkToStr(number) {
    if(number>=6){
        return 'high'
    }else if(number>=4){
        return 'normal'
    }
    return 'low'
}
let state=work.map(item=>translateWorkToStr(item));
state=state.map(item=>[item,1])
let stateCount=reduceByKey(state,(left,right)=>left+right);
stateCount.map(item=>{item[2]=Math.floor(item[1]/24*100)+"%"})
console.log("24小时状态",work);
console.log("分析",stateCount);
console.log("----------------------------")
/*---------------------------------------------------------------------*/